# -------------------------------------------------------------------
# Purpose: Creates Table 1
# Author:  Max Posch, 25/07/2025
# Usage:   Source this script to generate the table.
# -------------------------------------------------------------------
# Check that required paths exist
stopifnot(dir.exists(pdataconfanalysis))
stopifnot(dir.exists(poutput))

# Load data
ivSurnameStocks <- fread(file.path(pdataconfanalysis, "ivSurnameStocksMetaphone1900BoundariesPanel18951940.csv"))
ivSurnameStocks <- ivSurnameStocks[year >= 1900]


# Panel regressions for each year
o <- list()
vars1 <- c("iv_lo_mp_adjp_t1880", "iv_lo_mp_adjp_t1895_r")
o <- append(o, list(feols(n_namelast_mp_adjp ~ .[vars1], ivSurnameStocks[year == 1900])))

vars2 <- c("I_d_to_I_mp_adjp_t1880_r", "I_d_to_I_mp_adjp_t1895_r")
o <- append(o, list(feols(n_namelast_mp_adjp ~ .[vars1] + .[vars2] | gisjoin_1900_coo_mp + namelast_mp_region, ivSurnameStocks[year == 1900])))

years <- c(1905, 1910, 1915, 1920, 1925, 1930)
for (y in years) {
  vars1 <- c(vars1, sprintf("iv_lo_mp_adjp_t%d_r", y - 5))
  o <- append(o, list(feols(n_namelast_mp_adjp ~ .[vars1], ivSurnameStocks[year == y])))
  vars2 <- c(vars2, sprintf("I_d_to_I_mp_adjp_t%d_r", y - 5))
  o <- append(o, list(feols(n_namelast_mp_adjp ~ .[vars1] + .[vars2] | gisjoin_1900_coo_mp + namelast_mp_region, ivSurnameStocks[year == y])))
}

vars1 <- c(vars1, c("iv_lo_mp_adjp_t1930_r"))
o <- append(o, list(feols(n_namelast_mp_adjp ~ .[vars1], ivSurnameStocks[year == 1940])))

vars2 <- c(vars2, "I_d_to_I_mp_adjp_t1930_r")
o <- append(o, list(feols(n_namelast_mp_adjp ~ .[vars1] + .[vars2] | gisjoin_1900_coo_mp + namelast_mp_region, ivSurnameStocks[year == 1940])))


# F-statistics
f <- lapply(o, function(x) wald(x, keep = "iv_lo_mp_adjp", vcov = ~namelast_mp))
fstat <- as.character(round(unlist(lapply(f, function(x) x$stat))))


# Create table
setFixest_dict(c(
  n_namelast_mp_adjp = "No. of people in county $i$ with surname $k$ in year:",
  iv_lo_mp_adjp_t1880 = "$I_{k, -r(i)}^{1880} \\times \\frac{I_{-o(k), i}^{1880}}{I_{-o(k)}^{1880}}$",
  iv_lo_mp_adjp_t1895_r = "$I_{k, -r(i)}^{1895} \\times \\frac{I_{-o(k), i}^{1895}}{I_{-o(k)}^{1895}}$",
  iv_lo_mp_adjp_t1900_r = "$I_{k, -r(i)}^{1900} \\times \\frac{I_{-o(k), i}^{1900}}{I_{-o(k)}^{1900}}$",
  iv_lo_mp_adjp_t1905_r = "$I_{k, -r(i)}^{1905} \\times \\frac{I_{-o(k), i}^{1905}}{I_{-o(k)}^{1905}}$",
  iv_lo_mp_adjp_t1910_r = "$I_{k, -r(i)}^{1910} \\times \\frac{I_{-o(k), i}^{1910}}{I_{-o(k)}^{1910}}$",
  iv_lo_mp_adjp_t1915_r = "$I_{k, -r(i)}^{1915} \\times \\frac{I_{-o(k), i}^{1915}}{I_{-o(k)}^{1915}}$",
  iv_lo_mp_adjp_t1920_r = "$I_{k, -r(i)}^{1920} \\times \\frac{I_{-o(k), i}^{1920}}{I_{-o(k)}^{1920}}$",
  iv_lo_mp_adjp_t1925_r = "$I_{k, -r(i)}^{1925} \\times \\frac{I_{-o(k), i}^{1925}}{I_{-o(k)}^{1925}}$",
  iv_lo_mp_adjp_t1930_r = "$I_{k, -r(i)}^{1930} \\times \\frac{I_{-o(k), i}^{1930}}{I_{-o(k)}^{1930}}$",
  gisjoin_1900_coo_mp = "Origin country-County", namelast_mp_region = "Surname-Census division"
))

tablename <- file.path(poutput, "table01.tex")
etable(o,
  cluster = ~namelast_mp,
  file = tablename, replace = TRUE,
  headers = rep(c("1900", "1905", "1910", "1915", "1920", "1925", "1930", "1940"), each = 2),
  keep = "!Intercept",
  fitstat = ~ n + r2, digits = "r3", digits.stats = 3,
  group = list("-_$I_{i}^{\\tau} / I^{\\tau}$ controls" = c("I_d_to_I_mp_adjp_t1880_r", "I_d_to_I_mp_adjp_t1895_r", "I_d_to_I_mp_adjp_t1900_r", "I_d_to_I_mp_adjp_t1905_r", "I_d_to_I_mp_adjp_t1910_r", "I_d_to_I_mp_adjp_t1915_r", "I_d_to_I_mp_adjp_t1920_r", "I_d_to_I_mp_adjp_t1925_r", "I_d_to_I_mp_adjp_t1930_r")),
  extralines = list(c("$F$-stat", fstat)),
  style.tex = style.tex("aer"), tex = TRUE
)
add_table_row(tablename, "\\multicolumn\\{2\\}\\{c\\}\\{1900\\}", "\\cmidrule(lr){2-3} \\cmidrule(lr){4-5} \\cmidrule(lr){6-7} \\cmidrule(lr){8-9} \\cmidrule(lr){10-11} \\cmidrule(lr){12-13} \\cmidrule(lr){14-15} \\cmidrule(lr){16-17}")
move_estimates_rows(tablename, "^\\s*Constant\\s*&", "\\$F\\$-stat")
edit_table_content(tablename, "& \\$F\\$-stat", "$F$-stat")
cat("Table 1 saved to:", tablename, "\n")
